package com.itheima.service.impl;

import com.itheima.common.PageInfo;
import com.itheima.dto.OrderUpdateDto;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public PageInfo<Order> page(String number, String beginTime, String endTime, Integer page, Integer pageSize) {
        //创建一个容器
        PageInfo<Order> pageInfo = new PageInfo<Order>();
        //分页公式    第几页 = （第几页-1） * 一页的个数
        Integer start = (page - 1) * pageSize;
        List<Order> orders = orderMapper.page(number, beginTime, endTime, start, pageSize);

        //records：当前页面内容，是个数组[]
        pageInfo.setRecords(orders);
        //size：每页显示条数
        pageInfo.setSize(pageSize);
        //current：当前页码
        pageInfo.setCurrent(page);


        //计算 total(总条数)(通过名字) 的方法
        long count = orderMapper.countOrder();

        //total：总条数
        pageInfo.setTotal(count);
        return pageInfo;
    }

    @Override
    public boolean updateOrder(OrderUpdateDto orderUpdateDto) {

        if (orderUpdateDto == null) {
            throw new RuntimeException("参数不能为空");
        }

        int res = orderMapper.updateOrder(orderUpdateDto);

        return res == 1;
    }
}
